package cn.ava.ymcc;

import cn.ava.ymcc.utils.CodeGenerateUtils;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradePagePayResponse response = Payment.Page().pay(
                    "超级大火箭",
                    CodeGenerateUtils.generateOrderSn(2353L),
                    "2353.00",
                    "http://127.0.0.1:6002/detail.html?courseId=9");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
                System.out.println(response.getBody());
            } else {
                System.err.println(response);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com";
        config.signType = "RSA2";
        config.appId = "2021000121649572";
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtY4C6n6NwcQtLQ/D8fMLDQC9Qe6l32QMLLjP3JfsCr8Iidze5RDc4Q42v6G+EC2fZO4LwH/2ADc6b1m3TAIZju86EDz3kssibRUp5LmeTKrGwRNwVA+YR/mMcb6nKHsOM1WMyXjg8SP4JFrxndfJOpmvPAy6lpIw1t6WNSFOW+wBH5c95vQJ1LUTmGcMIaBVAT8q0arLaHRJr/KIxiykkNqT7LV4YT/Slbd6ChAmMu8L1fNcFNG5bdk8nAnNNtFCKeQuzIRZ7ZNHOjp6KDGpSs8uWojOzJhmXGH8JtaBCJEWzx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cl6B0mYhD7Rgda3hFF/Tcc5Wl0uxSwRAoGAF2dYsGBKIv8hUzskArOtWQ0KTJ5RFyNoUueSfYD++GWFMOraLLXwsYlgL0q3jRn0jG1XymXYfF1lxtB9y8g+Gk03e1If0wH3DKhJ9xgAYCaU8KEXWWIPbmUuGA/4et488GvbR9QzYcEOR1K4XeV+arAumsgD6Nj0pb8rh9Sgoy0=\n";
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3q7iUPZ9EsVIkiyZEcE31KfE3FH6zIXFChP11DwHzoQ/KehYzd2FfmP1DO8U6Q4n3JK/GhpTpHcd4fcRf2iP150FJCNMlFdrNr9NPMhNtVqPN+qEnFy4PSBq9i4LlxEUmMQhKBYkY9unh6M3yfxIkIBj9exp8aHgSI7DWHHhqKSKbiSiK0u6R5/gtDwvODk4bBwgdpJohaFWifxIrKmQVbMerVHdjkMVorjyOKer1de3mDivX/oCgWzSUZ7TkYDjY8Jk9bLp+vDT3SPrrJZy/4voYUOftTp7Vk2ouPL9YTEE2AbDGzrGo7jZUZCMsoW1pYou1qNrehCo2PH/bS4wXwIDAQAB";
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "https://www.baidu.com";
        return config;
    }
}